package com.luhai.shipmentscheck.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luhai.shipmentscheck.R;

/**
 * Created by jzh-创世头盔之父.
 * Date: 2019\10\28 0028* Time: 8:51
 * Emaile: 1186832904@qq.com
 * Introduction: jzh de code
 */
public class TopBar extends RelativeLayout implements View.OnClickListener {

    private Context mContext;

    private View view;

    private LinearLayout ll_left, ll_right;

    private ImageView iv_left, iv_right;

    private TextView tv_center;

    private OnTopBarClickListener onTopBarClickListener;

    public void setOnTopBarClickListener(OnTopBarClickListener onTopBarClickListener) {
        this.onTopBarClickListener = onTopBarClickListener;
    }

    public interface OnTopBarClickListener {
        void onTopBarClick(boolean isLeft);
    }

    public TopBar(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {

        view = LayoutInflater.from(mContext).inflate(R.layout.view_topbar, this, true);
        iv_left = view.findViewById(R.id.iv_left);
        iv_right = view.findViewById(R.id.iv_right);
        tv_center = view.findViewById(R.id.tv_center);
        ll_left = view.findViewById(R.id.ll_left);
        ll_right = view.findViewById(R.id.ll_right);
        ll_left.setOnClickListener(this);
        ll_right.setOnClickListener(this);
    }

    public void setVisible(boolean isLeft, boolean isShow) {
        (isLeft ? ll_left : ll_right).setVisibility(isShow ? VISIBLE : GONE);
    }

    public void setImageResource(boolean isLeft, int image) {
        (isLeft ? iv_left : iv_right).setImageResource(image);
    }

    public void setText(String text) {
        tv_center.setText(text);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_left:
                if (onTopBarClickListener != null) {
                    onTopBarClickListener.onTopBarClick(true);
                }
                break;
            case R.id.ll_right:
                if (onTopBarClickListener != null) {
                    onTopBarClickListener.onTopBarClick(false);
                }
                break;
        }
    }
}
